package com.pinda.controller;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinda.commonutils.Page;
import com.pinda.oms.entity.PdOrder;
import com.pinda.oms.entity.PdOrderAndTransport;
import com.pinda.service.IPdOrderService;
import com.pinda.work.entity.PdTransportOrder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@RestController
@RequestMapping("/pd_order")
public class PdOrderController {

    @Resource
    private IPdOrderService ipOrderService;
    //条件查询跟分页查询
    @PostMapping("/findOrder")
    public PageInfo<PdOrder> findOrder(@RequestBody Page<PdOrder> page){
        System.out.println(page.getSearch().getReceiverCountyId());
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<PdOrder> list=ipOrderService.queryone(page.getSearch());
        PageInfo<PdOrder> pageInfo=new PageInfo(list);
        return pageInfo;
    }
    //根据orderId查询订单
    @PostMapping("/findOrderTransport")
    public PageInfo<PdOrderAndTransport> findOrderTransport(@RequestBody Page<PdOrderAndTransport> page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        PageInfo<PdOrderAndTransport> pageInfo=new PageInfo(this.ipOrderService.findOrderTransport(page.getSearch()));
        System.out.println(pageInfo);
        return pageInfo;

    }

    public static String loadJSON(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }
   private static final String AK="2VcuGov8mnOI5xVY8GIoEeKjXaYLt7g9";
    // 调用百度地图API根据地址，获取坐标
    @GetMapping("/getCoordinate/{address}")
    public static String getCoordinate(@PathVariable("address") String address) {
        if (address != null && !"".equals(address)) {
            address = address.replaceAll("\\s*", "").replace("#", "栋");
            String url = "http://api.map.baidu.com/geocoding/v3/?output=json&ak=" + AK + "&callback=showLocation&address=" + address;
//            String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + AK;
            String json = loadJSON(url);
            json = StringUtils.substringBetween(json, "showLocation(", ")");
            if (json != null && !"".equals(json)) {
                Map map = JSON.parseObject(json, Map.class);
                if ("0".equals(map.getOrDefault("status", "500").toString())) {
                    Map childMap = (Map) map.get("result");
                    Map posMap = (Map) childMap.get("location");
                    double lng = Double.parseDouble(posMap.getOrDefault("lng", "0").toString()); // 经度
                    double lat = Double.parseDouble(posMap.getOrDefault("lat", "0").toString()); // 纬度
                    DecimalFormat df = new DecimalFormat("#.######");
                    String lngStr = df.format(lng);
                    String latStr = df.format(lat);
                    return lngStr + "," + latStr;
                }
            }
        }
        return null;
    }


}
